package org.zoomdev.zoom.dao.meta;

import org.apache.commons.lang3.ArrayUtils;
import org.zoomdev.zoom.dao.ddl.SqlType;

import java.util.Objects;

import static org.zoomdev.zoom.dao.utils.SqlUtils.escapeSql;

public final class ColumnMeta {

    private String comment;
    private String defaultValue;
    private String name;
    private SqlType type;
    private long size;
    private boolean notNull;
    private int scale;          //小数
    private boolean autoIncrement;
    private String[] values;

    public void toSql(StringBuilder sql, String[] primaryKeys, boolean pretty) {
        if (pretty) {
            sql.append("  ");
        }

        sql.append(getName()).append(" ").append(getType().name());

        // Add size/precision if applicable
        if (getSize() > 0) {
            sql.append("(").append(getSize());
            if (getScale() > 0) {
                sql.append(",").append(getScale());
            }
            sql.append(")");
        } else {
            if (getValues() != null) {
                if (getType() == SqlType.ENUM || getType() == SqlType.SET) {
                    sql.append("(");
                    int index = 0;
                    for (String str : getValues()) {
                        if (index > 0) {
                            sql.append(",");
                        }
                        ++index;
                        sql.append("'").append(str).append("'");
                    }

                    sql.append(")");
                }
            }
        }

        if (isNotNull()) {
            sql.append(" NOT NULL");
        }

        // Primary key notation
        if (primaryKeys != null && primaryKeys.length == 1 && ArrayUtils.contains(primaryKeys, getName())) {
            sql.append(" PRIMARY KEY");
        }

        if (getDefaultValue() != null) {
            sql.append(" DEFAULT ");
            if (getType().isString()) {
                sql.append("'").append(escapeSql(getDefaultValue())).append("'");
            } else {
                sql.append(escapeSql(getDefaultValue()));
            }
        }

        if (isAutoIncrement()) {
            sql.append(" AUTO_INCREMENT");
        }

        if (getComment() != null && !getComment().isEmpty()) {
            sql.append(" COMMENT '").append(escapeSql(getComment())).append("'");
        }


    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public SqlType getType() {
        return type;
    }

    public void setType(SqlType type) {
        this.type = type;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public boolean isNotNull() {
        return notNull;
    }

    public void setNotNull(boolean notNull) {
        this.notNull = notNull;
    }

    public int getScale() {
        return scale;
    }

    public void setScale(int scale) {
        this.scale = scale;
    }

    public boolean isAutoIncrement() {
        return autoIncrement;
    }

    public void setAutoIncrement(boolean autoIncrement) {
        this.autoIncrement = autoIncrement;
    }

    public String[] getValues() {
        return values;
    }

    public void setValues(String[] values) {
        this.values = values;
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof ColumnMeta)) return false;
        ColumnMeta that = (ColumnMeta) o;
        return size == that.size && notNull ==
                that.notNull && scale == that.scale
                && autoIncrement == that.autoIncrement
                && Objects.equals(comment, that.comment)
                && Objects.equals(defaultValue, that.defaultValue)
                && Objects.equals(name, that.name)
                && type == that.type
                && Objects.deepEquals(values, that.values);
    }


}